<?php
/**
 * Created by PhpStorm.
 * User: Sinri
 * Date: 2017/9/23
 * Time: 14:07
 */

namespace sinri\wenli;


use DOMDocument;

class DutyForKougo extends PageReader
{
    public function duty()
    {
        $this->dutyOld();
        $this->dutyNew();
    }

    public function dutyOld()
    {
        $chapters = [
            "創世記(口語訳)",
            "出エジプト記(口語訳)",
            "レビ記(口語訳)",
            "民数記(口語訳)",
            "申命記(口語訳)",
            "ヨシュア記(口語訳)",
            "士師記(口語訳)",
            "ルツ記(口語訳)",
            "サムエル記上(口語訳)",
            "サムエル記下(口語訳)",
            "列王紀上(口語訳)",
            "列王紀下(口語訳)",
            "歴代志上(口語訳)",
            "歴代志下(口語訳)",
            "エズラ記(口語訳)",
            "ネヘミヤ記(口語訳)",
            "エステル記(口語訳)",
            "ヨブ記(口語訳)",
            "詩篇(口語訳)",
            "箴言(口語訳)",
            "伝道の書(口語訳)",
            "雅歌(口語訳)",
            "イザヤ書(口語訳)",
            "エレミヤ書(口語訳)",
            "哀歌(口語訳)",
            "エゼキエル書(口語訳)",
            "ダニエル書(口語訳)",
            "ホセア書(口語訳)",
            "ヨエル書(口語訳)",
            "アモス書(口語訳)",
            "オバデヤ書(口語訳)",
            "ヨナ書(口語訳)",
            "ミカ書(口語訳)",
            "ナホム書(口語訳)",
            "ハバクク書(口語訳)",
            "ゼパニヤ書(口語訳)",
            "ハガイ書(口語訳)",
            "ゼカリヤ書(口語訳)",
            "マラキ書(口語訳)",
        ];

        for ($i = 0; $i < count($chapters); $i++) {
            $parts = explode("(", $chapters[$i]);
            $chapter_id = $i + 1;
            if ($chapter_id < 10) $chapter_id = "0" . $chapter_id;
            $chapter_name = $parts[0];
            $chapter_count = $this->bookChapterMapping($i) . "章";
            $this->dutyForBook($chapter_id, $chapter_name, $chapter_count);
        }
    }

    public function dutyNew()
    {
        $chapters = [
            "マタイによる福音書(口語訳)",
            "マルコによる福音書(口語訳)",
            "ルカによる福音書(口語訳)",
            "ヨハネによる福音書(口語訳)",
            "使徒行伝(口語訳)",
            "ローマ人への手紙(口語訳)",
            "コリント人への第一の手紙(口語訳)",
            "コリント人への第二の手紙(口語訳)",
            "ガラテヤ人への手紙(口語訳)",
            "エペソ人への手紙(口語訳)",
            "ピリピ人への手紙(口語訳)",
            "コロサイ人への手紙(口語訳)",
            "テサロニケ人への第一の手紙(口語訳)",
            "テサロニケ人への第二の手紙(口語訳)",
            "テモテヘの第一の手紙(口語訳)",
            "テモテヘの第二の手紙(口語訳)",
            "テトスヘの手紙(口語訳)",
            "ピレモンヘの手紙(口語訳)",
            "ヘブル人への手紙(口語訳)",
            "ヤコブの手紙(口語訳)",
            "ペテロの第一の手紙(口語訳)",
            "ペテロの第二の手紙(口語訳)",
            "ヨハネの第一の手紙(口語訳)",
            "ヨハネの第二の手紙(口語訳)",
            "ヨハネの第三の手紙(口語訳)",
            "ユダの手紙(口語訳)",
            "ヨハネの黙示録(口語訳)",
        ];

        for ($i = 0; $i < count($chapters); $i++) {
            $parts = explode("(", $chapters[$i]);
            $chapter_id = $i + 1 + 39;
            if ($chapter_id < 10) $chapter_id = "0" . $chapter_id;
            $chapter_name = $parts[0];
            $chapter_count = $this->bookChapterMapping($i) . "章";
            $this->dutyForBook($chapter_id, $chapter_name, $chapter_count);
        }
    }

    protected function getBookURL($book_id, $book_name = null, $chapter_id = null)
    {
        if (intval($book_id, 10) <= 39) {
            $url = "https://ja.wikisource.org/wiki/" . urlencode($book_name) . "(%E5%8F%A3%E8%AA%9E%E8%A8%B3)";
        } else {
            $url = "https://ja.wikisource.org/wiki/" . urlencode($book_name) . "(%E5%8F%A3%E8%AA%9E%E8%A8%B3)";
        }
        return $url;
    }

    protected function html2txt($html, $chapter = null)
    {
        $doc = new DOMDocument();
        @$doc->loadHTML($html);
        $element1 = $doc->getElementById("mw-content-text");
        $element2 = $element1->firstChild;

        $chapters = [];
        $currentChapter = null;
        $currentVerse = null;
        $word = null;
        for ($i = 0; $i < $element2->childNodes->length; $i++) {
            $element3 = $element2->childNodes->item($i);
            if (!isset($element3->tagName)) {
                continue;
            }
            if ($element3->tagName == 'h4') {
                $id = $element3->firstChild->getAttribute("id");
                if (preg_match('/^(\d+):(\d+)$/', $id, $matches)) {
                    $currentChapter = $matches[1];
                    $currentVerse = $matches[2];
                }
            } elseif ($element3->tagName == 'p') {
                if ($currentChapter && $currentVerse) {
                    $word = $element3->textContent;

                    if (!isset($chapters[$currentChapter])) {
                        $chapters[$currentChapter] = "";
                    }

                    $chapters[$currentChapter] .= "{$currentChapter}:{$currentVerse} " . $word . PHP_EOL;
                }
            }
        }

        return $chapters;
    }

    public function version()
    {
        return "kougo";
    }
}